/*
 * Created on 30.01.2008
 * @author Jens Anuth
*/
package albareth.tools;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class TilesetIndexer extends JFrame
{
    
    private String tileset = "tileset.png";

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        TilesetIndexer indexer = new TilesetIndexer();
        indexer.setSize(500,500);
        indexer.show();

    }
    
    public void paint(Graphics g)
    {
        super.paint(g);
        
        int ax = 60, ay = 150;
        int bx = 250, by = 50;
        for (int i = 0; i < 400; i++)
        {
            for (int j = 0; j < 400; j++)
            {
                if (distancePointSegment(ax, ay, bx, by, i, j) < 50)
                {
                    g.setColor(Color.GREEN);
                } else {
                    g.setColor(Color.RED);
                }
                g.drawRect(i, j, 1, 1);
            }
        }
        g.setColor(Color.WHITE);
        g.drawLine(ax, ay, bx, by);
        //g.fillRect(ax-1, ay-1, 3, 3);
        //g.fillRect(bx-1, by-1, 3, 3);
    }
    
    public static double distancePointPoint(double ax, double ay, double bx, double by)
    {
        return Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
    }
    
    
    /**
     * Taken from com.vividsolutions.jts.algorithm.CGAlgorithms
     */
    public static double distancePointSegment(double ax, double ay, double bx,
                                              double by, double px, double py)
    {

          // if start==end, then use pt distance
          //if (  A.equals(B) ) return p.distance(A);
        
        if (ax == bx && ay == by) return distancePointPoint(ax, ay, px, py);

          // otherwise use comp.graphics.algorithms Frequently Asked Questions method
          /*(1)             AC dot AB
                         r = ---------
                               ||AB||^2
              r has the following meaning:
              r=0 P = A
              r=1 P = B
              r<0 P is on the backward extension of AB
              r>1 P is on the forward extension of AB
              0<r<1 P is interior to AB
          */

          double r = ( (px - ax) * (bx - ax) + (py - ay) * (by - ay) )
                    /
                  ( (bx - ax) * (bx - ax) + (by - ay) * (by - ay) );

          if (r <= 0.0) return distancePointPoint(px, py, ax, ay);
          if (r >= 1.0) return distancePointPoint(px, py, bx, by);


          /*(2)
                   (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
              s = -----------------------------
                              L^2

              Then the distance from C to P = |s|*L.
          */

          double s = ((ay - py) *(bx - ax) - (ax - px)*(by - ay) )
                    /
                  ((bx - ax) * (bx - ax) + (by - ay) * (by - ay) );

          return
            Math.abs(s) *
            Math.sqrt(((bx - ax) * (bx - ax) + (by - ay) * (by - ay)));

    
    }

}
